#-*-coding: utf-8-*-


import sys
import os
from PyQt4 import QtGui
from PyQt4 import QtCore
from xml.dom import minidom


#fileroot = {'handbook':'/Users/congl/Documents/handbook'}
fileroot = {}
menudict={}
windowx = 0
#urltag = '/' #mac
urltag = '\\' #win


config = minidom.parse('resources/config.xml')
file = config.getElementsByTagName('file')[0]
dirs = file.getElementsByTagName('dirs')

for i in dirs:
    fileroot[i.getAttribute('name')] = i.childNodes[0].toxml()



        



  

#def loadICON():
#    p = os.path.join(os.path.dirname(os.path.realpath(__file__)), "te.ico")
#    return QtGui.QIcon(p)

class Task(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(100, 100, 640, 480)
        #self.setFixedSize(640, 480)
        self.box = QtGui.QHBoxLayout()
        
        #icon设置
        
        self.icon = QtGui.QSystemTrayIcon(QtGui.QIcon('resources/piao.ico'), self)
        self.menu = QtGui.QMenu(self)
#        
#        
#        #菜单设置        
        self.setFileMenu() #创建文件菜单
        self.setSysMenu() #设定系统功能菜单
        
        self.icon.show()
        
        self.setTab()
        
    
    def setTab(self):
        self.tab = QtGui.QTabWidget()
        #er = QtGui.QLabel('PyQt\nLabel') 
        #self.setting = self.tab.addTab(er,'setting')
        self.setQuickFile()
        
        okBotton = QtGui.QPushButton('OK', self)
        okBotton.setGeometry(540,440, 80, 20)
        self.box.addWidget(okBotton)
        
        #快捷文件
        splitter = QtGui.QSplitter(self)
        splitter.setGeometry(20, 10, 600, 420)
        splitter.addWidget(self.tab)
        
        
    def setQuickFile(self):
        setDirTable = QtGui.QTableWidget(10,2)
        setDirTable.setHorizontalHeaderLabels([u'菜单标示', u'路径'])
        setDirTable.setColumnWidth(0, 100)
        setDirTable.setColumnWidth(1, 200)
        setDirTable.verticalHeader().setVisible(False) #左侧表头不可见
        
        
        settingbox = self.tab.addTab(setDirTable, u'设置目录') 
        
        
  
    def setFileMenu(self): 
        for dictname in fileroot:
            handbook = self.menu.addMenu(dictname )
            filedir = os.walk(fileroot[dictname] )
            
            for roots, dirs, files in filedir:

                for dir in dirs:
                    if(menudict.has_key(roots)):
                        menudict[roots + urltag + dir] = menudict[roots].addMenu(dir )
                    
                    else:
                        menudict[roots + urltag + dir] = handbook.addMenu(dir )
                        
                    
                for file in files:
                    if(menudict.has_key(roots)):
                        if '.DS_Store' != file:
                            fileMeun = menudict[roots].addAction(file )
                            fileMeun.triggered.connect(self.openFile)
                            fileMeun.setObjectName(roots +urltag+file )
        
                                   
                    else:
                        if '.DS_Store' != file:
                            fileMeun = handbook.addAction(file )
                            fileMeun.triggered.connect(self.openFile)
                            fileMeun.setObjectName(roots +urltag+file )
        
            self.icon.setContextMenu(self.menu)
        
        
    def setSysMenu(self):
        self.menu.addSeparator()
        #设置
        #settingMenu = self.menu.addAction(u'设置')
        #settingMenu.triggered.connect(self.setting)
         
        #退出
        quitMenu = self.menu.addAction(u'退出')
        quitMenu.triggered.connect(self.quit)      
         
    #打开文件
    def openFile(self):
        sender = self.sender()
        str =  'start ' + unicode(sender.objectName()) + ''
        type = sys.getfilesystemencoding()
#        os.system('open "' + unicode(sender.objectName()) + '"') #MAc
        os.system(str .encode(type)) #win

 

           
    def quit(self):
#        sys.exit()
        self.close()
        
        
    def  setting(self, event):
       windowx =  self.cursor().pos().x()
       self.setGeometry(windowx-320, 0, 640, 480)

        
  
        

        
        
app = QtGui.QApplication(sys.argv)
task = Task()
#task.show()
sys.exit(app.exec_())

